//
//  GMDownloadPreviewController.m
//  gentleman
//
//  Created by 范月成 on 2017/12/20.
//  Copyright © 2017年 fancy. All rights reserved.
//

#import "GMDownloadPreviewController.h"
#import "GMArtworkItem.h"

@implementation GMArtCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self.contentView addSubview:self.imageView];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
}

- (void)setImgUrl:(NSString *)imgUrl {
    _imgUrl = imgUrl;
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:imgUrl]
                      placeholderImage:nil];
}


- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [UIImageView new];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        _imageView.userInteractionEnabled = YES;
        [_imageView setClipsToBounds:YES];
    }
    return _imageView;
}

@end

@interface GMDownloadPreviewController () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *artworksList;
@property (nonatomic, strong) NSMutableArray *datasource;

@end

@implementation GMDownloadPreviewController

+ (void)load {
    [MGJRouter registerURLPattern:kRouterDownloadList
                        toHandler:^(NSDictionary *routerParameters) {
                            GMArtwork *artwork = routerParameters[MGJRouterParameterUserInfo][@"artwork"];
                            GMDownloadPreviewController *download = [[GMDownloadPreviewController alloc] init];
                            download.artwork = artwork;
                            download.hidesBottomBarWhenPushed = YES;
                            [[UIViewController currentNavigationViewController] pushViewController:download animated:YES];
                        }];
}

- (void)dealloc {
    [[SDImageCache sharedImageCache].config setShouldDecompressImages:YES];
    [[SDWebImageDownloader sharedDownloader] setShouldDecompressImages:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = self.artwork.title;
    
    [self loadDatas];
    
    [[SDImageCache sharedImageCache].config setShouldDecompressImages:NO];
    [[SDWebImageDownloader sharedDownloader] setShouldDecompressImages:NO];
}

- (void)loadDatas {
    self.datasource = [NSMutableArray array];
    NSArray *arts = [GMArtworkItem objectsWhere:@"WHERE gid = ? ORDER BY pageIndex" arguments:@[@(self.artwork.gid)]];
    [self.datasource addObjectsFromArray:arts];
    [self.artworksList reloadData];
}

- (void)initSubviews {
    [super initSubviews];
    [self.view addSubview:self.artworksList];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    [self.artworksList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(self.safeAreaTop, 0, self.safeAreaBottom, 0));
    }];
}

- (UICollectionView *)artworksList {
    if (!_artworksList) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        CGFloat itemWH = (kScreenWidth - 3*4)/4;
        layout.itemSize = CGSizeMake(itemWH, itemWH);
        layout.minimumLineSpacing = 4;
        layout.minimumInteritemSpacing = 4;
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _artworksList = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _artworksList.collectionViewLayout = layout;
        _artworksList.delegate = self;
        _artworksList.dataSource = self;
        _artworksList.backgroundColor = [UIColor whiteColor];
        [_artworksList registerClass:[GMArtCell class] forCellWithReuseIdentifier:@"GMArtCell"];
    }
    return _artworksList;
}

#pragma mark - CollectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.datasource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GMArtCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GMArtCell" forIndexPath:indexPath];
    
    GMArtworkItem *item = self.datasource[indexPath.row];
    
    cell.imgUrl = item.imageUrl;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    GMArtCell *currentCell = (GMArtCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    NSMutableArray *items = [NSMutableArray array];

    for (NSInteger i = 0; i<self.datasource.count; i++) {
        GMArtCell *cell = (GMArtCell *)[collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
        YYPhotoGroupItem *photo = [YYPhotoGroupItem new];
        GMArtworkItem *item = self.datasource[i];
        photo.thumbView = cell.imageView;
        photo.largeImageURL = [NSURL URLWithString:item.imageUrl];
        [items addObject:photo];
    }
    
    YYPhotoGroupView *groupView = [[YYPhotoGroupView alloc] initWithGroupItems:items];
    groupView.blurEffectBackground = NO;
    
    [groupView presentFromImageView:currentCell.imageView toContainer:self.navigationController.view animated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
